#ifndef __ALGORISM_H
#define __ALGORISM_H	

#include <stdint.h>

#define square(x) x*x
#define cube(x) x*x*x
#define fsgn(x) ( (fabs(x)<1e-6) ? 0:(0<x)-(x<0) )

/**
 * \brief 平滑滤波器结构体 
 */
typedef struct
{
    float alpha; 
    float last, current;
}SmoothFilter;


/** @brief 窗口平滑滤波器(需全局初始化，带变量，指定变量地址)
* @note k[0 <----> last_data_num] = k[current <----> l...last] \n
* @note last_data[0 <----> last_data_num - 1] = last_data[last <----> l...last] \n
* @note last_data数组size = k数组size - 1 \n
* @example initialize e.g. :
* float powerFilterWeight[4] = {0.1, 0.2, 0.3 ,0.4}; 
* float powerFilterLast[3] = {0};
* SlidingWindowFilter PowerFilter =
* {
*    .last_data_num = 3,
*    .k = powerFilterWeight,
*    .last_data = powerFilterLast
* }; 
*/
typedef struct
{
    uint8_t last_data_num;
    float* const k;//sum of k_i equal to 1 essentially
	float* const last_data;
}SlidingWindowFilter;

void SmoothFilterInitialize(SmoothFilter* filter, float alpha);
float SmoothFilterUpdate(SmoothFilter* filter, float input);

float SlidingWindowFilterFilterUpdate(SlidingWindowFilter *filter, float input);

float AngleLimit(float angle, float limit_min, float limit_max);
float AbsLimiter(float val, float max);
float DoubleEdgeLimiter(float val, float min, float max);
float InvSqrt(float x);
int Sign(float x);
#endif
